<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('数据集列表')"/>
    <style>
        .status-btn {
            display: inline-block;
            padding: 1px 20px;
            margin: 0px;
            margin-top: 5px;
            background-color: #5bc0de; /* 可以根据需要调整颜色 */
            color: white;
            text-decoration: none;
            border-radius: 5px;
            transition: background-color 0.3s;
        }

        .status-btn:hover {
            background-color: #31b0d5; /* 悬停时的颜色变化 */
        }

        /*按钮下间距*/
        .button-spacing {
            margin-bottom: 5px; /* 根据需要调整 */
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            数据集名称：<input type="text" name="name"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="deep_learning:app01Dataset:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <li>
                <!--                通过点击按钮来切换显示的数据集状态-->
                <a class="status-btn" data-status="self">仅自己</a>
                <a class="status-btn" data-status="all">全部</a>
            </li>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" class="table table-bordered" data-mobile-responsive="true"></table>
        </div>
    </div>
</div>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('deep_learning:app01Dataset:edit')}]];
    var removeFlag = [[${@permission.hasPermi('deep_learning:app01Dataset:remove')}]];
    var prefix = ctx + "deep_learning/app01Dataset";

    var status = "self"; // 默认显示自己的数据集
    var currentUserId = null;
    var options = null;
    var optionsAdmin = null;

    var tableStatus = '仅自己';
    // 发起请求获取当前用户的 userId
    var userId = null;

    // 根据不同的请求来刷新
    function refreshWithOptions(url) {

        //普通用户显示的表格
        options = {
            // url: prefix + "/listWithParas",
            // url: prefix + "/list",
            // url: prefix + "/listWithOnlyUserId",
            url: url,
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            // removeUrl: prefix + "/delete",
            exportUrl: prefix + "/export",
            modalName: "数据集",
            showExport: true,
            sidePagination: 'client', // 前端分页
            columns: [
                {
                    field: 'id',
                    title: 'id',
                    visible: false
                },
                {
                    field: 'name',
                    title: '数据集名称',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'userName',
                    title: '归属用户',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'numTrain',
                    title: '图片数量',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'numVall',
                    title: '标签数量',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'height',
                    title: '高',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'width',
                    title: '宽',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'uploadTime',
                    title: '上传时间',
                    align: 'center',
                    sortable: true
                },

                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        // user_id和自己的userId一致才可以编辑和删除
                        if (row.userId === currentUserId) {
                            actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.removeDataset(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        }
                        actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="$.operate.viewFiles(\'' + row.location + '\')"><i class="fa fa-eye"></i>查看</a>'); // 新增的“查看”按钮
                        return actions.join(' ');
                    }
                }]
        };
        //管理员显示的表
        optionsAdmin = {
            // url: prefix + "/listWithParas",
            // url: prefix + "/list",
            // url: prefix + "/listWithOnlyUserId",
            url: url,
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            // removeUrl: prefix + "/delete",
            exportUrl: prefix + "/export",
            modalName: "数据集",
            showExport: true,
            sidePagination: 'client', // 前端分页
            columns: [
                {
                    field: 'id',
                    title: 'id',
                    visible: false
                },
                {
                    field: 'name',
                    title: '数据集名称',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'userName',
                    title: '归属用户',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'numTrain',
                    title: '图片数量',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'numVall',
                    title: '标签数量',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'height',
                    title: '高',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'width',
                    title: '宽',
                    align: 'center',
                    sortable: true
                },
                {
                    field: 'uploadTime',
                    title: '上传时间',
                    align: 'center',
                    sortable: true
                },

                // {
                // 	field : 'location',
                // 	title : '数据集位置',
                // 	align: 'center'
                // },
                // {
                //     field : 'isPublic',
                //     title : '是否公开',
                //     align: 'center'
                // },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        // user_id和自己的userId一致才可以编辑和删除
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.removeDataset(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
                        actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="$.operate.viewFiles(\'' + row.location + '\')"><i class="fa fa-eye"></i>查看</a>'); // 新增的“查看”按钮
                        return actions.join(' ');
                    }
                }]
        };

        if (currentUserId != 1) {
            $.table.destroy()
            $.table.init(options);
        } else {
            $.table.destroy()
            $.table.init(optionsAdmin);
        }
        var refreshButton = $("button[name='refresh']");
        console.log(refreshButton)
        //解除按钮点击事件绑定
        refreshButton.off("click");
        refreshButton.click(function () {
            console.log("refreshButton")
            console.log("tableStatus", tableStatus)
            switch (tableStatus) {
                case '仅自己':
                    $.table.destroy();
                    refreshWithOptions(prefix + "/listWithOnlyUserId")
                    break;
                case '用户全部':
                    $.table.destroy();
                    refreshWithOptions(prefix + "/listWithParasGet")
                    break;
                case '管理员全部':
                    $.table.destroy();
                    refreshWithOptions(prefix + "/list")
                    break;
            }
        });
    }


    $.ajax({
        url: '/deep_learning/app01Dataset/currentUser', // 此地址返回当前用户的 userId
        method: 'GET',
        success: function (response) {
            console.log("response.userId", response.userId)
            currentUserId = response.userId;
        },
        error: function (xhr, status, error) {
            console.error('请求当前用户信息失败:', error);
        }
    }).then(function () {
        // $.table.init(options);
        tableStatus = '仅自己';
        refreshWithOptions(prefix + "/listWithOnlyUserId")
    });


    // 给所有状态按钮绑定点击事件
    $(".status-btn").click(function () {
        // 获取按钮上的状态属性
        status = $(this).data("status");
        // console.log("status: " + status);
        if (status === "self") {
            // 仅自己
            console.log("status", status)
            // refreshTableWithOnlyUserId(currentUserId);
            // bindRefreshButton(currentUserId)
            tableStatus = '仅自己'
            refreshWithOptions(prefix + "/listWithOnlyUserId")
            // $.table.init(options)
        } else if (status === "all") {
            // 直接刷新
            if (currentUserId != 1) {
                tableStatus = '用户全部';
                refreshWithOptions(prefix + "/listWithParasGet")
            } else {
                console.log("点击了管理员全部")
                tableStatus = '管理员全部';
                refreshWithOptions(prefix + "/list")
            }
        }
    });
    window.addEventListener('message', function (event) {
        console.log("收到模型来自子窗口的消息：")
        setTimeout(function () {
            let refreshButton = $("button[name='refresh']");
            refreshButton.click();
        }, 20)
    });
</script>
</body>
</html>